package com.cskaoyan.com.collection.作业;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;

public class Ex1 {
    public static void main(String[] args) {

        Collection<User> collection = new ArrayList<>();
        collection.add(new User("zs", 18));
        collection.add(new User("zs", 18));
        collection.add(new User("ls", 18));
        collection.add(new User("ls", 18));
        collection.add(new User("wu", 18));
        collection.add(new User("wu", 18));


        // new User("zs", 18);   name  age一样
        // 取出重复的User.
        // 什么叫重复?  相对概念

        // 定义一个不存储重复的数据的容器
        Collection<User> newCollection = new ArrayList<>();

        Iterator<User> iterator = collection.iterator();

        while (iterator.hasNext()){
            User next = iterator.next();

            // 新容器里面  是否 contains存在这个  遍历的数据
            if (! newCollection.contains(next))
                newCollection.add(next);
        }

        System.out.println(newCollection);// 不重复的数据
    }
}

class User{
    String name;
    int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}